//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.oauth
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	/**
	 * 网易微博API 
	 * 1.1.13 Oauth
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoOauth extends DoAPI
	{
		public function DoOauth(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_REQUEST_TOKEN:String = 'http://api.t.163.com/oauth/request_token';
		public static const CMD_REQUEST_TOKEN:String = 'cmdRequestToken';
		
		private static const URL_AUTH_TOKEN:String = 'http://api.t.163.com/oauth/authorize';
		public static const CMD_AUTH_TOKEN:String = 'cmdAuthToken';
		
		private static const URL_ACCESS_TOKEN:String = 'http://api.t.163.com/oauth/access_token';
		public static const CMD_ACCESS_TOKEN:String = 'cmdAccessToken';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function getRequestToken(appKey:String, appSecret:String, callback:String = null):void
		{
			var oauthKey:OauthNeteaseKey = new OauthNeteaseKey();
			oauthKey.consumerKey = appKey;
			oauthKey.consumerSecret = appSecret;
			if (callback == null)
				callback = 'null';
			oauthKey.callbackUrl = OauthUrlUtil.executeString(callback);
			
			Oauth.oauthingKey = oauthKey;
			Oauth.oauthingKey.httpMethod = "GET";
			
			var paras:Array = [];
			var url:String = OauthUrlUtil.getOauthUrl(URL_REQUEST_TOKEN, 
													  oauthKey.httpMethod, 
													  oauthKey.consumerKey, 
													  oauthKey.consumerSecret, 
													  oauthKey.tokenKey, 
													  oauthKey.tokenSecrect, 
													  oauthKey.verify, 
													  oauthKey.callbackUrl, 
													  paras);
			send(url, resultHandler, errortHandler);
			
			function resultHandler(data:String):void
			{
				var params:Object = executeResponse(data);
				onDataHandler(CMD_REQUEST_TOKEN, params);
			}
			
			function errortHandler(data:String):void
			{
				_errorHandler(CMD_REQUEST_TOKEN, data);
			}
		}
		
		public function authRequestToken():void
		{
			var url:String = URL_AUTH_TOKEN + '?oauth_token=' + Oauth.oauthingKey.tokenKey;
			var urlReq:URLRequest = new URLRequest(url);
			navigateToURL(urlReq);
		}
		
		public function getAccessToken(verify:String):void
		{
			if (!Oauth.oauthingKey)
				return;
			
			var paras:Array = [];
			Oauth.oauthingKey.verify = verify;
			Oauth.oauthingKey.callbackUrl = null;
			Oauth.oauthingKey.httpMethod = "GET";
			var url:String = OauthUrlUtil.getOauthUrl(URL_ACCESS_TOKEN, 
				Oauth.oauthingKey.httpMethod, Oauth.oauthingKey.consumerKey, 
				Oauth.oauthingKey.consumerSecret, Oauth.oauthingKey.tokenKey, 
				Oauth.oauthingKey.tokenSecrect, Oauth.oauthingKey.verify, 
				Oauth.oauthingKey.callbackUrl, paras);
			send(url, resultHandler, errortHandler);
			
			function resultHandler(data:String):void
			{
				var params:Object = executeResponse(data);
				onDataHandler(CMD_ACCESS_TOKEN, params);
			}
			
			function errortHandler(data:String):void
			{
				_errorHandler(CMD_ACCESS_TOKEN, data);
			}
		}
		
		private function executeResponse(data:String):Object
		{
			var params:Object = {};
			var datas:Array = data.split("&");
			for(var i:int = 0; i < datas.length; i++){
				var key:String = datas[i].split("=")[0];
				var value:String = datas[i].split("=")[1];
				params[key] = value;
			}
			return params;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function onDataHandler(cmd:String, params:Object):void
		{
			switch (cmd)
			{
				case CMD_REQUEST_TOKEN:
					Oauth.oauthingKey.tokenKey = params.oauth_token;
					Oauth.oauthingKey.tokenSecrect = params.oauth_token_secret;
					break;
				case CMD_ACCESS_TOKEN:
					Oauth.currentAccountKey = Oauth.oauthingKey;
					Oauth.currentAccountKey.tokenKey = params.oauth_token;
					Oauth.currentAccountKey.tokenSecrect = params.oauth_token_secret;
					Oauth.currentAccountKey.verify = null;
					break;
			}
			
			_dataHandler(cmd, params);
		}
	}
}